/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.common.Size;

import java.util.Locale;

/**
 * ShapeSizeImageShaperTestPageView
 */
public class ShapeSizeImageShaperTestPageView extends DirectionalLayout implements Component.ClickedListener {
    private SketchImageView mImageResize;
    private Slider mSliderWidth;
    private Slider mSliderHeight;
    private Button mPreviousClickedBtn;
    private Image.ScaleMode mScaleType = Image.ScaleMode.ZOOM_CENTER;

    private int mImageWidth = 0;
    private int mImageHeight = 0;

    public ShapeSizeImageShaperTestPageView(Context context) {
        super(context, null);
        initView();
    }

    private void initView() {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        Component view = layoutScatter.parse(ResourceTable.Layout_shape_size_image_shaper_page_view, this, true);

        mImageResize = (SketchImageView) view.findComponentById(ResourceTable.Id_image_resizeFragment);
        mImageResize.setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                mImageWidth = component.getRight() - component.getLeft();
                mImageHeight = component.getBottom() - component.getTop();
                apply();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });

        Text textWidth = (Text) view.findComponentById(ResourceTable.Id_text_resizeFragment_width);
        Text textHeight = (Text) view.findComponentById(ResourceTable.Id_text_resizeFragment_height);

        mSliderWidth = (Slider) view.findComponentById(ResourceTable.Id_slider_width).findComponentById(ResourceTable.Id_slider);
        mSliderWidth.setMaxValue(1000);
        mSliderWidth.setValueChangedListener(new ValueChangedListener(textWidth));
        mSliderWidth.setProgressValue(500);

        mSliderHeight = (Slider) view.findComponentById(ResourceTable.Id_slider_height).findComponentById(ResourceTable.Id_slider);
        mSliderHeight.setMaxValue(1000);
        mSliderHeight.setValueChangedListener(new ValueChangedListener(textHeight));
        mSliderHeight.setProgressValue(500);

        Button btResizeFitStart = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_fixStart);
        Button btResizeFitCenter = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_fixCenter);
        Button btResizeFitEnd = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_fixEnd);
        Button btResizeFitXY = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_fixXY);
        Button btResizeCenter = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_center);
        Button btResizeCenterCrop = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_centerCrop);
        Button btResizeCenterInside = (Button) view.findComponentById(ResourceTable.Id_button_resizeFragment_centerInside);

        btResizeFitStart.setTag(Image.ScaleMode.ZOOM_START);
        btResizeFitCenter.setTag(Image.ScaleMode.ZOOM_CENTER);
        btResizeFitEnd.setTag(Image.ScaleMode.ZOOM_END);
        btResizeFitXY.setTag(Image.ScaleMode.STRETCH);
        btResizeCenter.setTag(Image.ScaleMode.CENTER);
        btResizeCenterCrop.setTag(Image.ScaleMode.CLIP_CENTER);
        btResizeCenterInside.setTag(Image.ScaleMode.INSIDE);

        btResizeFitStart.setClickedListener(this);
        btResizeFitCenter.setClickedListener(this);
        btResizeFitEnd.setClickedListener(this);
        btResizeFitXY.setClickedListener(this);
        btResizeCenter.setClickedListener(this);
        btResizeCenterCrop.setClickedListener(this);
        btResizeCenterInside.setClickedListener(this);
        btResizeFitCenter.simulateClick();
    }

    private void apply() {
        if (mImageWidth == 0 || mImageHeight == 0) {
            return;
        }
        Size size = Utils.getPixmapSize(mSliderWidth.getProgress(), mSliderHeight.getProgress(),
                mImageWidth, mImageHeight);
        mImageResize.getOptions().setResize(size.width, size.height, mScaleType);
        mImageResize.displayAssetImage(AssetImage.MEI_NV);
    }

    @Override
    public void onClick(Component component) {
        Button button = (Button) component;
        button.setEnabled(false);
        button.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_button_element_pressed));
        button.setTextColor(new Color(Color.rgb(174, 174, 174)));

        if (mPreviousClickedBtn != null) {
            mPreviousClickedBtn.setEnabled(true);
            mPreviousClickedBtn.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_button_element_normal));
            mPreviousClickedBtn.setTextColor(Color.BLACK);
        }

        mPreviousClickedBtn = button;
        mScaleType = (Image.ScaleMode) component.getTag();
        apply();
    }

    private class ValueChangedListener implements Slider.ValueChangedListener {
        private final Text text;

        public ValueChangedListener(Text text) {
            this.text = text;
        }

        @Override
        public void onProgressUpdated(Slider slider, int i1, boolean b1) {
            if (i1 < 200) {
                slider.setProgressValue(200);
                return;
            }

            text.setText(String.format("%d/%d", i1, 1000, Locale.ENGLISH));
        }

        @Override
        public void onTouchStart(Slider slider) {

        }

        @Override
        public void onTouchEnd(Slider slider) {
            apply();
        }
    }
}